package com.kugong.user;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.kugong.battle.Battle;
import com.kugong.match.Match;
import com.kugong.signUp.SignUpValidator;
import com.kugong.util.CommonUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/8/5.
 */
public class UserController extends Controller {
    @Clear
    public void index() {
        //查询最近三场比赛信息
        Page<Match> matchs = Match.dao.paginate(1, 3);
        setAttr("matchs",matchs);
        //查询积分前10
        List players = User.dao.qryBestPlayer();
        setAttr("players",players);
        //查询历届冠军10名
        List<Battle> battles = Battle.dao.qryChampionBattle();
        List<HashMap> champions = new ArrayList<HashMap>();
        if(battles!=null){
            for (int i = 0; i < battles.size(); i++) {
                HashMap data = new HashMap();
                Battle battle = battles.get(i);
                Battle champion = Battle.dao.qryChampion(battle.getStr("groupNo"), battle.getStr("matchId"));
                data.put("name",battle.getStr("name"));
                data.put("signName",champion.getStr("signName"));
                champions.add(data);
            }
        }
        setAttr("champions",champions);
        render("login.jsp");
    }

    @Clear
    public void login() {
        User user = getModel(User.class);
        String open_id = getPara("open_id");
        if (user.get("name") != null && user.get("passWord") != null) {
            String name = user.getStr("name").trim();
            String password = user.getStr("passWord").trim();
            User user1 = User.dao.qryUserByName(name);

            if (user1 != null) {
                String password1 = user1.getStr("passWord");
                if (password.equals(password1)) {
                    setSessionAttr("name", name);
                    setSessionAttr("userId", user1.get("id"));
                    setSessionAttr("signName", user1.get("signName"));
                    setSessionAttr("level", user1.get("level"));


                    if(open_id!=null&&!open_id.trim().equals("")){
                        Weixin weixin = Weixin.dao.findFirst("select * from tb_weixin where open_id=?",open_id);
                        if(weixin!=null){
                            weixin.delete();
                        }
                        Weixin.dao.addWeixin(open_id,name,password);
                    }

                    redirect("/match");
                    return;
                }
            }

        }
        setAttr("titleMsg", "账号或密码错误！");
        if(open_id!=null&&!open_id.trim().equals("")) {
            setAttr("open_id", getPara("open_id"));
        }

        /*******这段代码在以后网站进行ajax改版时进行调整*******/
        //查询最近三场比赛信息
        Page<Match> matchs = Match.dao.paginate(1, 3);
        setAttr("matchs",matchs);
        //查询积分前10
        List players = User.dao.qryBestPlayer();
        setAttr("players",players);
        //查询历届冠军10名
        List<Battle> battles = Battle.dao.qryChampionBattle();
        List<HashMap> champions = new ArrayList<HashMap>();
        if(battles!=null){
            for (int i = 0; i < battles.size(); i++) {
                HashMap data = new HashMap();
                Battle battle = battles.get(i);
                Battle champion = Battle.dao.qryChampion(battle.getStr("groupNo"), battle.getStr("matchId"));
                data.put("name",battle.getStr("name"));
                data.put("signName",champion.getStr("signName"));
                champions.add(data);
            }
        }
        setAttr("champions",champions);
        /******这段代码在以后网站进行ajax改版时进行调整********/

        render("login.jsp");
    }

    @Clear
    public void weixinLogin(){
        String open_id = getPara();
        User user = User.dao.qryWeixinUser(open_id);
        if(user!=null){
            setSessionAttr("name", user.getStr("name"));
            setSessionAttr("userId", user.get("id"));
            setSessionAttr("signName", user.get("signName"));
            setSessionAttr("level", user.get("level"));
            redirect("/match");
        }else{
            setAttr("open_id", open_id);
            render("login.jsp");
        }

    }

    public void logout(){
        removeSessionAttr("name");
        setSessionAttr("signName", "游客");
        redirect("/");
    }

    @Clear
    @Before(UserValidator.class)
    public void register() {
        User user = getModel(User.class);
        if (user.get("name") != null && user.get("passWord") != null) {
            String name = user.getStr("name").trim();
            User user1 = User.dao.qryUserByName(name);
            if (user1 == null) {
                user.set("create_time", new Date());
                user.save();
                setAttr("titleMsg", "注册成功，请登录。");
                render("login.jsp");
            } else {
                setAttr("titleMsg", "该用户已存在，请使用其他用户名。");
                render("register.jsp");
            }
        } else {
            setAttr("titleMsg", "用户名必须填写。");
            setAttr("contentMsg", "密码必须填写。");
            render("register.jsp");
        }
    }

    public void myHome(){
        String name = getSessionAttr("name").toString();
        User user = User.dao.qryUserByName(name);
        setAttr("user",user);

        render("index.jsp");
    }

    public void changeSelf(){
        String  signName = getPara("signName");
        String name = getSessionAttr("name").toString();
        User user = User.dao.qryUserByName(name);
        if(user.get("modify_time")!=null) {//有值说明已经修改过，进入付费修改流程
            Date modify_time = user.getDate("modify_time");
            if (modify_time.before(CommonUtil.lastMonth())) {//判断一个月之内是否修改过
                if(user.getInt("points")>10){//判断积分是否超过10分
                    user.set("signName", signName).set("modify_time", new Date()).set("points",user.getInt("points")-10).update();
                    redirect("/myHome");
                }else{
                    redirect("/myHome");
                }
            }else{
                redirect("/myHome");
            }
        }else{//无值说明没修改过直接修改
            user.set("signName", signName).set("modify_time",new Date()).update();
            redirect("/myHome");
        }
    }
}
